package com.rockxin.cat.activity;

import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.SPUtils;
import com.rockxin.cat.utils.ToastUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by cat on 18-3-2.
 */

public class ModifyPaymentPasswdActivity extends BaseActivity {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.btn_send_code)
    Button btnSendCode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_passwd)
    EditText etPasswd;
    @BindView(R.id.et_confirm_passwd)
    EditText etConfirmPasswd;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private Boolean isSendCode = false;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
        isSendCode = false;
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_reset_login_passwd;
    }

    @Override
    public void initData() {
        initView();
        setListener();
    }

    public void initView() {
        title.setText("修改支付密码");
        etPhone.setSingleLine();
        etCode.setSingleLine();
        etPasswd.setSingleLine();
        etConfirmPasswd.setSingleLine();

        etPasswd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        etConfirmPasswd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    private void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btnSendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isMobileNO()) {
                    ToastUtils.showShortToastSafe("手机号不正确");
                    return;
                }
                if (!isSendCode) {
                    isSendCode = true;
                    timer.start();
                    final CatNetWork catNetWork = new CatNetWork();
                    catNetWork.sendCodeReset(etPhone.getText().toString(), new Subscriber<HttpResult>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(HttpResult httpResult) {
                            Logger.e("CatNetWork------->" + httpResult.toString());
                            ToastUtils.showShortToastSafe(httpResult.getInfo());
                        }
                    });
                }
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });
    }

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            btnSendCode.setText((millisUntilFinished / 1000) + "秒后可重发");
        }

        @Override
        public void onFinish() {
            isSendCode = false;
            btnSendCode.setText("获取验证码");
        }
    };

    private boolean isMobileNO() {
        final String phone = etPhone.getText().toString();
        Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(phone);
        boolean bool = !m.matches();
        return bool;
    }


    private void submit() {
        final String phone = etPhone.getText().toString();
        final String passwd = etPasswd.getText().toString();
        final String confirmPasswd = etConfirmPasswd.getText().toString();
        final String code = etCode.getText().toString();
        Logger.d(passwd.length());
        if (isMobileNO()) {
            ToastUtils.showShortToastSafe("手机号不正确");
            return;
        } else if (passwd.length() < 6) {
            ToastUtils.showShortToastSafe("密码不能小于6位");
            return;
        } else if (confirmPasswd.length() < 6) {
            ToastUtils.showShortToastSafe("重复密码不能小于6位");
            return;
        } else if (!passwd.equals(confirmPasswd)) {
            ToastUtils.showShortToastSafe("密码不一致");
            return;
        } else if (code.length() < 1) {
            ToastUtils.showShortToastSafe("验证码不能为空");
            return;
        }
        final CatNetWork catNetWork = new CatNetWork();
        catNetWork.modifyPaypwd(phone, passwd, passwd, code, new Subscriber<HttpResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult httpResult) {
                Logger.e("CatNetWork------->" + httpResult.toString());
                if (httpResult.getStatus() == 1) {
                    etPhone.setText("");
                    etPasswd.setText("");
                    etConfirmPasswd.setText("");
                    etCode.setText("");
                }
                ToastUtils.showShortToastSafe(httpResult.getInfo());
            }
        });
    }

}
